package com.fingard.dsp.bank.directbank.bojz01;


import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class BOJZ01SinglePay extends BOJZ01Base implements ISinglePay {

    //单笔支付，不需采用流处理方式
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail tmpRetDetail;
        tmpRetDetail = externalPayments(p_req1908);
        if (tmpRetDetail.respCode.equals("-1") && StringHelper.isNullOrEmpty(tmpRetDetail.reqSeqID)) {
            tmpRetDetail.reqSeqID = p_req1908.singleDetail.askNumber;
        }
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    //190802对外支付
    private RetAtsSglDetail externalPayments(ReqSingle p_req1908) throws Exception {
        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        if (p_req1908.singleDetail.sameBnk.equals("1")) {//同行
            bankInnerTransfer(p_req1908, retDetail);
        } else {//跨行
            crossBankTransfer(p_req1908, retDetail);
        }
        return retDetail;
    }

    //同行支付
    private RetAtsSglDetail bankInnerTransfer(ReqSingle p_req1908, RetAtsSglDetail retDetail) throws Exception {
        transCode = "B2EInnerTransfer";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入报文头部信息
        writeCommonHeader(xmlWriter, "0");
        /*<Body>
		<PayerAcNo>8015014000000004444</PayerAcNo>
		<PayerAcName></PayerAcName>
		<PayeeAcNo>509010670308019</PayeeAcNo>
		<PayeeAcName>פ</PayeeAcName>
		<Amount>100000.00</Amount>
		<Currency>CNY</Currency>
		<Remark></Remark>
		<PayeeType>I</PayeeType>
		</Body>
		</Message>*/
        XElement tmpXElement = new XElement("Body", "");
        tmpXElement.addChild(new XElement("PayerAcNo", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("PayeeAcNo", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("PayeeAcName", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("Amount", p_req1908.singleDetail.payAmount));
        tmpXElement.addChild(new XElement("Remark", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("CifJnlNo", reqHeader.reqSeqID));
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndElement();
        xmlWriter.writeEndDocument();//报文结束
        //发送给银行的签名端口进行签名
        String[] tmpSign = sendToBankFrontSign(xmlWriter.toXmlString());
        if (tmpSign[0].length() == 0) {
            String signature = "";
            String body = "";
            String aa = "<result>";
            int i = tmpSign[1].indexOf(aa);
            if (i != -1) {
                body = tmpSign[1].substring(i + 8, i + 9);
            }
            if (body.equals("0")) {
                String ab = "<sign>";
                String ac = "</sign>";
                int ib = tmpSign[1].indexOf(ab);
                int ic = tmpSign[1].indexOf(ac);
                //以下为最终的签名结果
                signature = tmpSign[1].substring(ib + 6, ic);
                signature = clearLineWrap(signature);
                singData = signature;

                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter, "1");
                xmlTextWriter.writeXElement(tmpXElement);
                xmlTextWriter.writeEndElement();
                xmlTextWriter.writeEndDocument();//报文结束
                String[] tmpStrRet = sendToBank(xmlTextWriter.toXmlString());//发送给银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1].substring(tmpStrRet[1].indexOf("<?xml")), tmpSbMsg);
                    if (tmpBkReader != null) {
                        retDetail.respCode = "0";
                        retDetail.respInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                        retDetail.transState = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        retDetail.payInfoCode = retDetail.transState;
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpSbMsg.toString();
                    }
                } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpStrRet[1];
                    retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                    retDetail.transState = PaymentState.FAIL_Payment;
                    retDetail.payInfoCode = tmpStrRet[0];
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpStrRet[1];
                }
            } else {//签名数据返回数据为空
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSign[1];
            }
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpSign[1];
        }
        return retDetail;
    }

    //跨行支付
    private RetAtsSglDetail crossBankTransfer(ReqSingle p_req1908, RetAtsSglDetail retDetail) throws Exception {
        transCode = "B2ETransfer";
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter, "0");
		/*<Body>
		<PayerAcNo>521012735403012</PayerAcNo>
		<PayerAcName>˾</PayerAcName>
		<PayeeAcNo>0402024409300161221</PayeeAcNo>
		<PayeeAcName>800003208</PayeeAcName>
		<Amount>83.01</Amount>
		<TrsMode>D</TrsMode>
		<Currency>CNY</Currency>
		<Remark></Remark>
		<PayeeBankName></PayeeBankName>
		<PayeeDeptName></PayeeDeptName>
		<PayeeDeptId>102121000391</PayeeDeptId>
		</Body>
		</Message>*/
        XElement tmpXElement = new XElement("Body", "");
        tmpXElement.addChild(new XElement("PayerAcNo", p_req1908.singleDetail.payAct));
        tmpXElement.addChild(new XElement("PayeeAcNo", p_req1908.singleDetail.recAct));
        tmpXElement.addChild(new XElement("PayeeAcName", p_req1908.singleDetail.recName));
        tmpXElement.addChild(new XElement("Amount", p_req1908.singleDetail.payAmount));

        tmpXElement.addChild(new XElement("Remark", p_req1908.singleDetail.purpose));
        tmpXElement.addChild(new XElement("UnionDeptId", p_req1908.singleDetail.cnapsCode));
        tmpXElement.addChild(new XElement("PayeeBankName", p_req1908.singleDetail.cnapsName));
        String trsMode = "1";//java版暂不支持网银互联功能 默认走人行大小额
        tmpXElement.addChild(new XElement("Remit", trsMode));//1-大额2-网银互联
        tmpXElement.addChild(new XElement("CifJnlNo", reqHeader.reqSeqID));
        xmlWriter.writeXElement(tmpXElement);
        xmlWriter.writeEndElement();
        xmlWriter.writeEndDocument();//写入报文完成
        //发送给银行的签名端口进行签名
        String[] tmpSign = sendToBankFrontSign(xmlWriter.toXmlString());
        if (tmpSign[0].length() == 0) {
            String signature = "";
            String body = "";
            String aa = "<result>";
            int i = tmpSign[1].indexOf(aa);
            if (i != -1) {
                body = tmpSign[1].substring(i + 8, i + 9);
            }
            if (body.equals("0")) {
                String ab = "<sign>";
                String ac = "</sign>";
                int ib = tmpSign[1].indexOf(ab);
                int ic = tmpSign[1].indexOf(ac);
                //以下为最终的签名结果
                signature = tmpSign[1].substring(ib + 6, ic);
                signature = clearLineWrap(signature);
                singData = signature;
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                writeCommonHeader(xmlTextWriter, "1");
                xmlTextWriter.writeXElement(tmpXElement);
                xmlTextWriter.writeEndElement();
                xmlTextWriter.writeEndDocument();//写入报文完成
                String[] tmpStrRet = sendToBank(xmlTextWriter.toXmlString());//发送银行
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    //解析银行返回
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1].substring(tmpStrRet[1].indexOf("<?xml")), tmpSbMsg);
                    if (tmpBkReader != null) {
                        retDetail.respCode = "0";
                        retDetail.respInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                        retDetail.transState = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        retDetail.payInfoCode = retDetail.transState;
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpSbMsg.toString();
                    }
                } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                    retDetail.respCode = "0";
                    retDetail.respInfo = tmpStrRet[1];
                    retDetail.reqSeqID = p_req1908.singleDetail.askNumber;
                    retDetail.transState = PaymentState.FAIL_Payment;
                    retDetail.payInfoCode = tmpStrRet[0];
                } else {
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpStrRet[1];
                }
            } else {//签名数据返回数据为空
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSign[1];
            }
        } else {
            retDetail.respCode = "-1";
            retDetail.respInfo = tmpSign[1];
        }
        return retDetail;
    }

    //1944 状态查询
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        //返回ATS的对象
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);

        RetAtsSglDetail retDetail = new RetAtsSglDetail();
        transCode = "B2ETransferResQry";
        String time1 = Format.Date8Format.format(new Date());
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        writeCommonHeader(xmlWriter, "0");//写入通用的头部节点
        xmlWriter.writeStartElement("Body");
        xmlWriter.writeElementString("CifJnlNo", reqHeader.reqSeqID);
        xmlWriter.writeElementString("TransDate", time1);
        xmlWriter.writeEndElement();
        xmlWriter.writeEndDocument();
        try {
            //发送给银行的签名端口进行签名
            String[] tmpSign = sendToBankFrontSign(xmlWriter.toXmlString());
            if (tmpSign[0].length() == 0) {
                String signature = "";
                String body = "";
                String aa = "<result>";
                int i = tmpSign[1].indexOf(aa);
                if (i != -1) {
                    body = tmpSign[1].substring(i + 8, i + 9);
                }
                if (body.equals("0")) {
                    String ab = "<sign>";
                    String ac = "</sign>";
                    int ib = tmpSign[1].indexOf(ab);
                    int ic = tmpSign[1].indexOf(ac);
                    //以下为最终的签名结果
                    signature = tmpSign[1].substring(ib + 6, ic);
                    signature = clearLineWrap(signature);
                    singData = signature;
                    XmlTextWriter xmlTextWriter = new XmlTextWriter();
                    writeCommonHeader(xmlTextWriter, "1");
                    xmlTextWriter.writeStartElement("Body");
                    xmlTextWriter.writeElementString("CifJnlNo", reqHeader.reqSeqID);
                    xmlTextWriter.writeElementString("TransDate", time1);
                    xmlTextWriter.writeEndElement();
                    xmlTextWriter.writeEndDocument();


                    String[] tmpStrRet = sendToBank(xmlTextWriter.toXmlString());
                    if (tmpStrRet[0].length() == 0) {
                        StringBuilder tmpSbMsg = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1].substring(tmpStrRet[1].indexOf("<?xml")), tmpSbMsg);
                        if (tmpBkReader != null) {
                            String returnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                            String returnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                            if (returnCode != null && isActiveRetCode(returnCode)) {
                                retDetail.respCode = "0";
                                retDetail.reqSeqID = p_req1944.askNumber;
                                retDetail.transAmt = tmpBkReader.getFirstText("/Message/Body/Amount");
                                retDetail.respInfo = tmpBkReader.getFirstText("/Message/Body/ErrorMsg");
                                retDetail.transState = returnCode + "+" + tmpBkReader.getFirstText("/Message/Body/Status");
                                retDetail.payInfoCode = retDetail.transState;
                            } else {
                                retDetail.respCode = "-1";
                                retDetail.respInfo = returnMsg;
                            }
                        } else {
                            retDetail.respCode = "-1";
                            retDetail.respInfo = tmpSbMsg.toString();
                        }
                    } else {
                        retDetail.respCode = "-1";
                        retDetail.respInfo = tmpStrRet[1];
                    }
                } else {//签名数据返回数据为空
                    retDetail.respCode = "-1";
                    retDetail.respInfo = tmpSign[1];
                }
            } else {
                retDetail.respCode = "-1";
                retDetail.respInfo = tmpSign[1];
            }
        } catch (Exception e) {
            retDetail.respCode = "-1";
            retDetail.respInfo = e.getMessage();
            e.printStackTrace();
        }

        retAts.writeRetDetail(retDetail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
